const fp = require('lodash/fp')
const _ = require('lodash')
const { Maybe, Container } = require('./support')

const trace = _.curry((tag, v) => {
  console.log(tag, v)
  return v
})

const cars = [
  {
      name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, in_stock: true
  },
  {
      name: 'Spyker C12 Zagato', horsepower: 650, dollar_value: 648000, in_stock: false
  },
  {
      name: 'Jaguar XKR-S', horsepower: 550, dollar_value: 132000, in_stock: false
  },
  {
      name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false
  },
  {
      name: 'Aston Martin One-77', horsepower: 750, dollar_value: 1850000, in_stock: true
  },
  {
      name: 'Pagani Huayra', horsepower: 700, dollar_value: 1300000, in_stock: false
  }
]

let isLastInStock = function (cars) {
  let fn = fp.flowRight(fp.prop('in_stock'), fp.last)
  return fn(cars)
}
console.log(isLastInStock(cars))

function getFirstCarName (cars) {
  let fn = fp.flowRight(fp.prop('name'), fp.first)
  return fn(cars)
}
console.log(getFirstCarName(cars))


let _underscore = fp.replace(/\W+/g, '_')
let sanitizeNames = function (arr) {
  let fn = fp.flowRight(fp.map((fp.flowRight(trace('map之后'),fp.join('_'), fp.map(fp.toLower), fp.split('_'), _underscore))))
  return fn(arr)
}
console.log(sanitizeNames(['hello world', 'jason huang']))

// 3.1

let maybe = Maybe.of([5, 6, 11])
let ex1 = () => {
  return maybe.map(fp.map(fp.add(1)))
}
console.log(ex1()._value)

// 3.2
let xs = Container.of(['do', 'ray', 'me', 'fa','so', 'la', 'si', 'do'])
let ex2 = () => {
  return xs.map(fp.first)
}
console.log(ex2()._value)

// 3.3
let safeProp = fp.curry(function (x, o) {
  return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
// console.log(safeProp('name')(user))
let ex3 = () => {
  return safeProp('name')(user).map(fp.first)
}
console.log(ex3()._value)

//3.4
let ex4 = function (n) {
 return Maybe.of(n).map(parseInt)
}
console.log(ex4(null))

